package com.demo.shardingsphere.util;

import java.util.Random;

public class OrderUtil {



    /**
     *  获取商户id后4位
     * @param supplierId
     * @return
     */
    public static int getSuffixBySupplierId(long supplierId){
        String value=String.valueOf(supplierId);
        int length=value.length();
        if(length>=2){
            value=value.substring(length-2,length);
        }
        return Integer.parseInt(value);
    }
    /**
     * 获取用户id后4位
     * @param userId
     * @return
     */
    public static int getUidSuffixByUserId(long userId){
        String value=String.valueOf(userId);
        int length=value.length();
        if(length>=4){
            value=value.substring(length-4,length);
        }

        return Integer.parseInt(value);
    }

    /**
     * 订单号规则 时间戳+userId后4位+supplierId后2位+随机数
     * @param userId
     * @param supplierId
     * @return
     */
    public static String getOrderNo( Long userId,Long supplierId){
        String time=""+System.currentTimeMillis();
        //格式化，不足补0
        String uid=String.format("%4d", getUidSuffixByUserId(userId)).replace(" ", "0");
        String sId=String.format("%2d", getSuffixBySupplierId(supplierId)).replace(" ", "0");
        Random ran = new Random();
        int i = ran.nextInt(10);
        String str=time+uid+sId+i ;
        return str;

    }



}
